<template>
  <div>
    <!-- dialog弹出框 -->
    <el-dialog
      title="批量审核"
      :visible.sync="show"
      width="50%"
      :before-close="closeDialog"
    >
      <!-- from表单 -->
      <div class="from-infomations">
        <el-form ref="form" label-position="left" :model="sendInfo" label-width="8rem">
          <el-form-item label="当前选中人数">
            {{ userNum }}人
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog()">取 消</el-button>
        <el-button type="primary" @click="sendDialog()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { batchScores } from '../../api/send.js'
import { isOpenSendTime } from '../../utils/isOpenSendTime.js'
export default {
  props: {
    // 状态获取
    getStatusType: {
      type: Array,
      default: () => {}
    }
  },
  data() {
    return {
      // 获取全部数据存储位置
      // 获取全部数据存储位置
      show: false,
      sendInfo: {},
      userInfo: {
        userId: '',
        userPwd: '',
        userType: '',
        userBranch: '',
        userName: '',
        userReconsider: '',
        userScore: ''
      },
      routerRules: {
        routerId: '',
        routerName: '',
        routerScoreType: '',
        routerBaseScore: '',
        routerMaxScore: '',
        routerMinScore: ''
      },
      allScore: [],
      // 代码数据存储位置
      sendStatus: '',
      scoreRules: '',
      scoreAllRule: [
        {
          value: '1',
          label: '加分制'
        },
        {
          value: '2',
          label: '扣分制'
        }
      ],
      scoreTips: '',
      sendScore: '',
      userNum: ''
    }
  },
  created() {
    // dialog信息初始化
    this.createDialogStatus()
  },
  methods: {
    // 展示dialog
    showDialog(data) {
      this.show = true
      var that = this
      //  获取用户信息
      that.statusType = data.allSend
      that.userNum = that.statusType.length
    },
    // 保存所有信息准备提交
    allAdd() {
      var that = this
      var flist = sessionStorage.getItem('flist')
      var slist = sessionStorage.getItem('slist')
      var tlist = sessionStorage.getItem('tlist')
      var userId = sessionStorage.getItem('userId')
      var sendSemseterId = sessionStorage.getItem('semId')
      // 封装数据
      for (let i = 0; i < that.statusType.length; i++) {
        that.allScore.push({
          userId: that.statusType[i].userId,
          flistId: flist,
          slistId: slist,
          tlistId: tlist,
          sendSemseterId: sendSemseterId,
          userName: userId,
          sendScore: ''
        })
      }
    },
    // 上传最终数据
    pushEndScore() {
      var that = this
      new Promise(function(resolve, reject) {
        batchScores(that.allScore).then((res) => {
          resolve(res.obj)
        })
      }).then((data) => {
        if (data === null) {
          that.show = false
          that.$message({
            message: '评分成功',
            type: 'success'
          })
          top.location.reload()
        }
      })
    },
    // 页面逻辑方法

    // dialog信息初始化
    createDialogStatus() {
      this.show = false
    },
    // 关闭dialog
    closeDialog() {
      this.$confirm('是否确定取消', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.show = false
        this.$message({
          type: 'success',
          message: '取消审核成功'
        })
      }).catch(() => {})
    },
    // 提交dialog
    sendDialog() {
      var that = this
      // 看下是否在开放时间内
      isOpenSendTime().then((res) => {
        that.timeType = res
        if (that.timeType === 1) {
          new Promise(function(resolve, reject) {
            that.$confirm('是否确定提交', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              that.$message('正在提交中')
              resolve()
            }).catch(() => {
              resolve()
            })
          }).then(() => {
            that.allAdd()
            if (that.allScore !== null) {
              that.pushEndScore()
            } else {
              that.$message({
                message: '当前服务器繁忙，请稍后再试',
                type: 'warning'
              })
            }
          })
        } else if (that.timeType === 2) {
          that.$message({
            message: '未到开放时间',
            type: 'warning'
          })
        } else if (that.timeType === 3) {
          that.$message({
            message: '已过开放时间',
            type: 'warning'
          })
        } else {
          that.$message({
            message: '系统繁忙请联系管理员',
            type: 'warning'
          })
        }
      })
    }
  }
}
</script>
<style lang="scss">

</style>
